import { routerRedux } from 'dva/router';
import { message } from 'antd';
import { isResponerCode } from '../utils/utils';
import {
  getBusinessDetails,
  getTaskList,
  showScreen,
  showHomeVisit,
  commitHomeVisit,
  pickCarVerify,
  getApprovalList,
  commitDoApproval,
  commitinComeVerify,
  commitCustomerVerify,
  commitAssetsVerify,
  commitinCreditVerify,
  commitScreen,
  commitScreen2,
  jumpTaskList,
  transferTaskList,
  stopTaskList,
  selectAssignor,
  getCustomerBalance,
  getCustomerPrice,
  getBankList,
  startCredit,
  getCreditResult,
  getReportList,
  selectCustManager,
  businnesSubmitToBank,
  queryTaskManageList,
  getSecretKey,
  goBackTaskList,
} from '../services/api';

export default {
  namespace: 'risk',

  state: {

    customers: [],
    riskTaskList: [],
    riskBusinessDetails: null,
    riskLoading: false,
    riskResult: {},
    riskPage: {},
    riskScreenDetails: null,
    riskHomeVisitDetails: null,
    riskHomeVisitLoading: false,
    riskApprovalList: null,
    customerDetails: null,
    selectAssignorList: null,
    auditStatusList: [false, false, false, false],
    customerBalance: null,
    customerPrice: null,
    riskBankList: null,
    riskCreditDate: null,
    riskReportList: null,
    riskCustManager: null,
    riskTaskManageList: null,
    getSecretKey: null,
  },

  effects: {
    //风险管理  获取业务详情（审核）
    *getTaskList({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskLoading: true,
        },
      });
      const response = yield call(getTaskList, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          riskTaskList: response.data.list,
          riskPage: response.data.page,
        },
      });
      if (callback) callback();
    },

    //风险管理  获取业务详情（审核）
    *queryTaskManageList({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskLoading: true,
        },
      });
      const response = yield call(queryTaskManageList, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          riskTaskManageList: response.data.list,
          riskPage: response.data.page,
        },
      });
      if (callback) callback();
    },

    //风险管理  获取业务详情（审核）
    *getBusinessDetails({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskLoading: true,
        },
      });
      const response = yield call(getBusinessDetails, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          riskBusinessDetails: response.data,
          auditStatusList: [
            response.data.verifyStatus && response.data.verifyStatus.split("")[0] == '1',
            response.data.verifyStatus && response.data.verifyStatus.split("")[1] == '1',
            response.data.verifyStatus && response.data.verifyStatus.split("")[2] == '1',
            response.data.verifyStatus && response.data.verifyStatus.split("")[3] == '1',
          ],
        },
      });
      if (callback) callback();
    },

    //风险管理  提车审核提交
    *pickCarVerify({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskLoading: true,
        },
      });
      const response = yield call(pickCarVerify, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          data: response.data,
          riskResult: response.result,
        },
      });
      if (callback) callback();
    },

    //风险管理  家访信息获取
    *showHomeVisit({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskLoading: true,
        },
      });
      const response = yield call(showHomeVisit, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitDetails: response.data,
        },
      });
      if (callback) callback();
    },

    //风险管理  业务初筛信息
    *showScreen({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskLoading: true,
        },
      });
      const response = yield call(showScreen, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          riskScreenDetails: response.data,
        },
      });
      if (callback) callback();
    },

    //风险管理 获取审核记录
    *getApprovalList({ payload, callback }, { call, put }) {
      const response = yield call(getApprovalList, payload);
      if (!isResponerCode(response)) {
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskApprovalList: response.data,
        },
      });
      if (callback) callback();
    },

    //风险管理  家访调查提交
    *commitHomeVisit({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(commitHomeVisit, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          data: response.data,
          riskResult: response.result,
        },
      });
      if (callback) callback();
    },


    //风险管理  审核(初审、复审)提交
    *commitDoApproval({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(commitDoApproval, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          data: response.data,
          riskResult: response.result,
        },
      });
      if (callback) callback();
    },

    //风险管理  收入核实提交
    *commitinComeVerify({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(commitinComeVerify, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          data: response.data,
          riskResult: response.result,
        },
      });
      if (callback) callback();
    },

    //风险管理  客户核实提交 
    *commitCustomerVerify({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(commitCustomerVerify, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          data: response.data,
          riskResult: response.result,
        },
      });
      if (callback) callback();
    },

    //风险管理  资产核实提交
    *commitAssetsVerify({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(commitAssetsVerify, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          data: response.data,
          riskResult: response.result,
        },
      });
      if (callback) callback();
    },
    //风险管理  资产核实提交
    *commitinCreditVerify({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(commitinCreditVerify, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          data: response.data,
          riskResult: response.result,
        },
      });
      if (callback) callback();
    },
    //风险管理  初筛提交-
    *commitScreen({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(commitScreen, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          data: response.data,
          riskResult: response.result,
        },
      });
      if (callback) callback();
    },

    //风险管理  初筛提交-
    *commitScreen2({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(commitScreen2, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          data: response.data,
          riskResult: response.result,
        },
      });
      if (callback) callback();
    },

    //任务管理-跳过当前任务
    *jumpTaskList({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(jumpTaskList, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          data: response.data,
          riskResult: response.result,
        },
      });
      if (callback) callback();
    },

    //任务管理-退回当前任务
    *goBackTaskList({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(goBackTaskList, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          data: response.data,
          riskResult: response.result,
        },
      });
      if (callback) callback();
    },

    //任务管理-终止任务
    *stopTaskList({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(stopTaskList, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          data: response.data,
          riskResult: response.result,
        },
      });
      if (callback) callback();
    },

    //任务管理-转派任务
    *transferTaskList({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(transferTaskList, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          data: response.data,
          riskResult: response.result,
        },
      });
      if (callback) callback();
    },

    //任务管理-转派任务列表
    *selectAssignor({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(selectAssignor, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          selectAssignorList: response.data,
          riskResult: response.result,
        },
      });
      if (callback) callback();
    },

    //获取客户余额
    *getCustomerBalance({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(getCustomerBalance, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          customerBalance: response.data,
        },
      });
      if (callback) callback();
    },

    //获取客户本次查询扣多少钱
    *getCustomerPrice({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(getCustomerPrice, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          customerPrice: response.data,
        },
      });
      if (callback) callback();
    },

    //获取支行列表
    *getBankList({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(getBankList, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          riskBankList: response.data,
        },
      });
      if (callback) callback();
    },

    //发起征信
    *startCredit({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(startCredit, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'apply/loadThirdPartyData',
          payload: false
        });
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          data: response.data,
          riskResult: response.result
        },
      });
      if (callback) callback();
    },

    //获取征信查询结果
    *getCreditResult({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(getCreditResult, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          riskCreditDate: response.data,
        },
      });
      if (callback) callback();
    },

    //获取支行列表
    *getReportList({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(getReportList, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          riskReportList: response.data.list,
          riskPage: response.data.page

        },
      });
      if (callback) callback();
    },

    //业务上报时选择银行客户经理
    *selectCustManager({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(selectCustManager, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          riskCustManager: response.data,
        },
      });
      if (callback) callback();
    },

    //业务上报
    *businnesSubmitToBank({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: true,
        },
      });
      const response = yield call(businnesSubmitToBank, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'save',
          payload: {
            riskHomeVisitLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'save',
        payload: {
          riskHomeVisitLoading: false,
        },
      });
      yield put({
        type: 'save',
        payload: {
          data: response.data,
          riskResult: response.result
        },
      });
      if (callback) callback();
    },
    
    //加密秘钥
    *getSecretKey({ payload, callback }, { call, put }) {
      const response = yield call(getSecretKey, payload);
      if (!isResponerCode(response)) {    
        message.error(response.result.message)    
        return false
      }
      yield put({
        type: 'save',
        payload: {
          getSecretKey: response.data,
        },
      });
      if (callback) callback();
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    saveStepFormData(state, { payload }) {
      return {
        ...state,
        step: {
          ...state.step,
          ...payload,
        },
      };
    },
    setCustomerDetails(state, action) {
      return {
        ...state,
        customerDetails: action.payload,
      };
    },

    changeApplyDetailsState1(state, action) {
      return {
        ...state,
        auditStatusList: action.payload,
      };
    },
  },
};
